#include <stdio.h>
#include <time.h>
#include "CifDataLoader.h"
#include "CifCrystalData.h"

void PrintCifData(CifData *cifData)
{
	int blocksCount = cifData->DataBlocks->GetCount();

	for (int i = 0; i < blocksCount; i++)
	{
		CifDataBlock* block = cifData->DataBlocks->GetItemAt(i);

		printf("DATA BLOCK: %s\n", block->Code);

		int dataItemsCount = block->DataItems->GetCount();

		printf("DATA ITEMS:\n\n");

		for (int j = 0; j < dataItemsCount; j++)
		{
			CifDataItem* dataItem = block->DataItems->GetItemAt(j);

			printf("DATA ITEM: Name = %s, value = %s\n", dataItem->Name, dataItem->Value);
		}

		printf("DATA TABLES:\n\n");

		int tablesCount = block->DataTables->GetCount();

		for (int j = 0; j < tablesCount; j++)
		{
			CifDataTable *dataTable = block->DataTables->GetItemAt(j);

			printf("DATA TABLE:\n");
			
			int columnsCount = dataTable->ColumnNames->GetCount();

			for (int k = 0; k < columnsCount; k++)
			{
				printf("%12s", dataTable->ColumnNames->GetItemAt(k));
				printf(" ");
			}

			printf("\n");

			int rowsCount = dataTable->Rows->GetCount();

			for (int k = 0; k < rowsCount; k++)
			{
				List<char *> *row = dataTable->Rows->GetItemAt(k);

				for (int l = 0; l < columnsCount; l++)
				{
					printf("%12s", row->GetItemAt(l));
					printf(" ");
				}
				
				printf("\n");
			}
		}

		printf("\n\n");
	}
}

int main()
{
	ACifDataLoader *loader = new CifDataLoader();

	time_t begin = time(NULL);
	CifData *cifData = loader->Load("../../test/121_k2pti6_pub.cif");
	time_t end = time(NULL);

	printf("TIME OF CIF LOADING = %ld seconds.\n\n", end - begin);

	if (cifData != NULL)
	{
		//PrintCifData(cifData);
	}

	CifCrystalData<double> *crystalData = new CifCrystalData<double>(cifData);

	delete(loader);
	delete(cifData);
	delete(crystalData);

	return 0;
}